﻿
namespace Indeco.G24.Windows.Forms
{
	/// <summary>
	/// abstract NguoiDungNhaCungCap typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class NguoiDungNhaCungCapDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<NguoiDungNhaCungCapDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.NguoiDungNhaCungCap _currentNguoiDungNhaCungCap = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxNguoiDungNhaCungCapDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxNguoiDungNhaCungCapErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxNguoiDungNhaCungCapBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the IDNguoiDung property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDNguoiDungDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDNhaCungCap property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDNhaCungCapDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.NhaCungCap> _IDNhaCungCapList;
		
		/// <summary> 
		/// The list of selectable NhaCungCap
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.NhaCungCap> IDNhaCungCapList
		{
			get {return this._IDNhaCungCapList;}
			set 
			{
				this._IDNhaCungCapList = value;
				this.uxIDNhaCungCapDataGridViewColumn.DataSource = null;
				this.uxIDNhaCungCapDataGridViewColumn.DataSource = this._IDNhaCungCapList;
			}
		}
		
		private bool _allowNewItemInIDNhaCungCapList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of NhaCungCap
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDNhaCungCapList
		{
			get { return _allowNewItemInIDNhaCungCapList;}
			set
			{
				this._allowNewItemInIDNhaCungCapList = value;
				this.uxIDNhaCungCapDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.NguoiDung> _IDNguoiDungList;
		
		/// <summary> 
		/// The list of selectable NguoiDung
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.NguoiDung> IDNguoiDungList
		{
			get {return this._IDNguoiDungList;}
			set 
			{
				this._IDNguoiDungList = value;
				this.uxIDNguoiDungDataGridViewColumn.DataSource = null;
				this.uxIDNguoiDungDataGridViewColumn.DataSource = this._IDNguoiDungList;
			}
		}
		
		private bool _allowNewItemInIDNguoiDungList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of NguoiDung
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDNguoiDungList
		{
			get { return _allowNewItemInIDNguoiDungList;}
			set
			{
				this._allowNewItemInIDNguoiDungList = value;
				this.uxIDNguoiDungDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.NguoiDungNhaCungCap> _NguoiDungNhaCungCapList;
				
		/// <summary> 
		/// The list of NguoiDungNhaCungCap to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.NguoiDungNhaCungCap> NguoiDungNhaCungCapList
		{
			get {return this._NguoiDungNhaCungCapList;}
			set
			{
				this._NguoiDungNhaCungCapList = value;
				this.uxNguoiDungNhaCungCapBindingSource.DataSource = null;
				this.uxNguoiDungNhaCungCapBindingSource.DataSource = value;
				this.uxNguoiDungNhaCungCapDataGridView.DataSource = null;
				this.uxNguoiDungNhaCungCapDataGridView.DataSource = this.uxNguoiDungNhaCungCapBindingSource;				
				//this.uxNguoiDungNhaCungCapBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxNguoiDungNhaCungCapBindingSource_ListChanged);
				this.uxNguoiDungNhaCungCapBindingSource.CurrentItemChanged += new System.EventHandler(OnNguoiDungNhaCungCapBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnNguoiDungNhaCungCapBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentNguoiDungNhaCungCap = uxNguoiDungNhaCungCapBindingSource.Current as Entities.NguoiDungNhaCungCap;
			
			if (_currentNguoiDungNhaCungCap != null)
			{
				_currentNguoiDungNhaCungCap.Validate();
			}
			//_NguoiDungNhaCungCap.Validate();
			OnCurrentEntityChanged();
		}

		//void uxNguoiDungNhaCungCapBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.NguoiDungNhaCungCap"/> instance.
		/// </summary>
		public Entities.NguoiDungNhaCungCap SelectedNguoiDungNhaCungCap
		{
			get {return this._currentNguoiDungNhaCungCap;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxNguoiDungNhaCungCapDataGridView.VirtualMode;}
			set
			{
				this.uxNguoiDungNhaCungCapDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxNguoiDungNhaCungCapDataGridView.AllowUserToAddRows;}
			set {this.uxNguoiDungNhaCungCapDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxNguoiDungNhaCungCapDataGridView.AllowUserToDeleteRows;}
			set {this.uxNguoiDungNhaCungCapDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxNguoiDungNhaCungCapDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxNguoiDungNhaCungCapDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="NguoiDungNhaCungCapDataGridViewBase"/> class.
		/// </summary>
		public NguoiDungNhaCungCapDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxNguoiDungNhaCungCapDataGridView = new System.Windows.Forms.DataGridView();
			this.uxNguoiDungNhaCungCapBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxNguoiDungNhaCungCapErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDNguoiDungDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDNhaCungCapDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			//this.uxIDNhaCungCapBindingSource = new NhaCungCapBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNhaCungCapBindingSource)).BeginInit();
			//this.uxIDNguoiDungBindingSource = new NguoiDungBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNguoiDungBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungNhaCungCapDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungNhaCungCapBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungNhaCungCapErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxNguoiDungNhaCungCapErrorProvider
			// 
			this.uxNguoiDungNhaCungCapErrorProvider.ContainerControl = this;
			this.uxNguoiDungNhaCungCapErrorProvider.DataSource = this.uxNguoiDungNhaCungCapBindingSource;						
			// 
			// uxNguoiDungNhaCungCapDataGridView
			// 
			this.uxNguoiDungNhaCungCapDataGridView.AutoGenerateColumns = false;
			this.uxNguoiDungNhaCungCapDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxNguoiDungNhaCungCapDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDNguoiDungDataGridViewColumn,
		this.uxIDNhaCungCapDataGridViewColumn			});
			this.uxNguoiDungNhaCungCapDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxNguoiDungNhaCungCapDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxNguoiDungNhaCungCapDataGridView.Name = "uxNguoiDungNhaCungCapDataGridView";
			this.uxNguoiDungNhaCungCapDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxNguoiDungNhaCungCapDataGridView.TabIndex = 0;	
			this.uxNguoiDungNhaCungCapDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxNguoiDungNhaCungCapDataGridView.EnableHeadersVisualStyles = false;
			this.uxNguoiDungNhaCungCapDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnNguoiDungNhaCungCapDataGridViewDataError);
			this.uxNguoiDungNhaCungCapDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNguoiDungNhaCungCapDataGridViewCellValueNeeded);
			this.uxNguoiDungNhaCungCapDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNguoiDungNhaCungCapDataGridViewCellValuePushed);
			
			//
			// uxIDNguoiDungDataGridViewColumn
			//
			this.uxIDNguoiDungDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDNguoiDungDataGridViewColumn.DataPropertyName = "IDNguoiDung";
			this.uxIDNguoiDungDataGridViewColumn.HeaderText = "IDNguoiDung";
			this.uxIDNguoiDungDataGridViewColumn.Name = "uxIDNguoiDungDataGridViewColumn";
			this.uxIDNguoiDungDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDNguoiDungDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDNguoiDungDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDNhaCungCapDataGridViewColumn
			//
			this.uxIDNhaCungCapDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDNhaCungCapDataGridViewColumn.DataPropertyName = "IDNhaCungCap";
			this.uxIDNhaCungCapDataGridViewColumn.HeaderText = "IDNhaCungCap";
			this.uxIDNhaCungCapDataGridViewColumn.Name = "uxIDNhaCungCapDataGridViewColumn";
			this.uxIDNhaCungCapDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDNhaCungCapDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDNhaCungCapDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDNhaCungCapDataGridViewColumn
			//				
			this.uxIDNhaCungCapDataGridViewColumn.DisplayMember = "NhaCungCap";	
			this.uxIDNhaCungCapDataGridViewColumn.ValueMember = "IDNhaCungCap";	
			this.uxIDNhaCungCapDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDNhaCungCapDataGridViewColumn.DataSource = uxIDNhaCungCapBindingSource;				
				
			//
			// uxIDNguoiDungDataGridViewColumn
			//				
			this.uxIDNguoiDungDataGridViewColumn.DisplayMember = "TenSuDung";	
			this.uxIDNguoiDungDataGridViewColumn.ValueMember = "IDNguoiDung";	
			this.uxIDNguoiDungDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDNguoiDungDataGridViewColumn.DataSource = uxIDNguoiDungBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxNguoiDungNhaCungCapDataGridView);
			this.Name = "NguoiDungNhaCungCapDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNhaCungCapBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNguoiDungBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungNhaCungCapErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungNhaCungCapDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungNhaCungCapBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				NguoiDungNhaCungCapDataGridViewEventArgs args = new NguoiDungNhaCungCapDataGridViewEventArgs();
				args.NguoiDungNhaCungCap = _currentNguoiDungNhaCungCap;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class NguoiDungNhaCungCapDataGridViewEventArgs : System.EventArgs
		{
			private Entities.NguoiDungNhaCungCap	_NguoiDungNhaCungCap;
	
			/// <summary>
			/// the  Entities.NguoiDungNhaCungCap instance.
			/// </summary>
			public Entities.NguoiDungNhaCungCap NguoiDungNhaCungCap
			{
				get { return _NguoiDungNhaCungCap; }
				set { _NguoiDungNhaCungCap = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxNguoiDungNhaCungCapDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungNhaCungCapDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxNguoiDungNhaCungCapDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungNhaCungCapDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNguoiDungNhaCungCapDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDNguoiDungDataGridViewColumn":
						e.Value = NguoiDungNhaCungCapList[e.RowIndex].IDNguoiDung;
						break;
					case "uxIDNhaCungCapDataGridViewColumn":
						e.Value = NguoiDungNhaCungCapList[e.RowIndex].IDNhaCungCap;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxNguoiDungNhaCungCapDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungNhaCungCapDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNguoiDungNhaCungCapDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDNguoiDungDataGridViewColumn":
						NguoiDungNhaCungCapList[e.RowIndex].IDNguoiDung = (System.Guid)e.Value;
						break;
					case "uxIDNhaCungCapDataGridViewColumn":
						NguoiDungNhaCungCapList[e.RowIndex].IDNhaCungCap = (System.Guid)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
